package easy;

public class test13 {
    public static void main(String[] args) {
        test13 test13 = new test13();
        System.out.println(test13.reformat("a0b1c2"));
    }

    public String reformat(String s) {
        StringBuilder word = new StringBuilder();
        StringBuilder num = new StringBuilder();
        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) >= 'a') {
                word.append(s.charAt(i));
            } else {
                num.append(s.charAt(i));
            }
        }

        int max = Math.max(word.length(), num.length());
        int min = Math.min(word.length(), num.length());
        if (max - min <= 1 && word.length() == max) {
            for (int i = 0; i < s.length();) {
                if (max - min > 0) {
                    sb.append(word.charAt(--max));
                    i++;
                }
                else {
                    sb.append(num.charAt(--min));
                    i++;
                }
            }
            return sb.toString();
        }

        if (max - min <= 1 && num.length() == max) {
            for (int i = 0; i < s.length();) {
                if (max - min > 0) {
                    sb.append(num.charAt(--max));
                    i++;
                }
                else {
                    sb.append(word.charAt(--min));
                    i++;
                }
            }
            return sb.toString();
        }


        return "";
    }
}
